package com.sapient;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sapient.DAO.CommodityCreateDAO;

/**
 * Servlet implementation class CommodityCreate
 */
@WebServlet("/CommodityCreate")
public class CommodityCreate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommodityCreate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html");
		String status = "";
		String commodityName=request.getParameter("commodityName");
		String startDate=request.getParameter("startDate");
		String endDate=request.getParameter("endDate");
		String commodityClass=request.getParameter("commodityClass");
		
		if(endDate != "")
			endDate = endDate.substring(0, 10);
		startDate = startDate.substring(0, 10);
		System.out.println("startDate"+startDate);
		System.out.println("EndDate"+endDate);
		System.out.println(commodityName);
		System.out.println("\n");
		System.out.println(commodityClass);
		System.out.println(startDate);
		System.out.println("\n");
		System.out.println(endDate);

		HttpSession session=request.getSession(true);
		String lastupdateduser=(String)session.getAttribute("User");
		
		if(CommodityCreateDAO.insertCommodity(commodityName,commodityClass,lastupdateduser,startDate,endDate))
			status = "Entity Created!";
		else
			status = "Entity creation failed. Commodity type already existed.";
		request.setAttribute("status", status);
		RequestDispatcher rd=getServletContext().getRequestDispatcher("/Commodity.jsp");
		rd.include(request,response);
		//PrintWriter pw=response.getWriter();
		//pw.println("successfully commodity added");
		
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
